package com.books4sale.admin.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONWriter;

import com.books4sale.admin.dto.tools.Helper;

public class DeleteBook extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//		String idString = req.getParameter(Helper.BOOK_ID);
		String rowIndex = req.getParameter(Helper.ROW_INDEX);
//		idString = idString.substring(idString.lastIndexOf("(")+1, idString.lastIndexOf(")"));
		Long id = Long.parseLong(req.getParameter(Helper.BOOK_ID));
		
		String result = Helper.get().deleteBook(id)?"success":"fail";
		
		resp.setContentType("application/json;charset=UTF-8");
		resp.setCharacterEncoding("UTF-8");
		
		JSONWriter jsonWriter = new JSONWriter(resp.getWriter());
		
		try {
			jsonWriter.object().key("result").value(result).
			key("rowIndex").value(rowIndex)
			.endObject();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
